import requests
from lxml import etree
import urllib.request

url = 'http://jandan.net/pic'

for i in range(3):
    print(url)
    req = requests.get(url)
    html = req.text

    tree = etree.HTML(html)
    result = tree.xpath('//a[@class="view_img_link"]/@href')

    for link in result:
        print('downloading', link)
        filename = link.split('/')[-1]
        urllib.request.urlretrieve('http:' + link, 'pics/' + filename)

    current_page = tree.xpath('//span[@class="current-comment-page"]/text()')
    next_page = int(current_page[0].strip('[]')) - 1

    url = 'http://jandan.net/pic/page-%d' % next_page

